package phoenix.web.page_objects;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseWebExt;
import phoenix.core.config.extensions.ConfigExt;
import phoenix.core.data_models.TestUserDataModel;
import phoenix.web.locators.base.SearchPageLocatorsBase;

public class ActivityPage extends PageBaseWebExt
{

	/**
	 * Initializes the ActivityPage
	 */
	public ActivityPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	public SearchPageLocatorsBase elements()
	{
		switch (manager.testModel().getSiteName())
		{
			case ConfigExt.SITE_ID_CLICKANDFLIRT:
			{
				return new phoenix.web.locators.clickandflirt.SearchPageLocators();
			}
			case ConfigExt.SITE_ID_LOCALSGOWILD:
			{
				return new phoenix.web.locators.localsgowild.SearchPageLocators();
			}
			case ConfigExt.SITE_ID_EBONYFLIRT:
			{
				return new phoenix.web.locators.ebonyflirt.SearchPageLocators();
			}
			case ConfigExt.SITE_ID_CHEECKYLOVERS:
			{
				return new phoenix.web.locators.cheekylovers.SearchPageLocators();
			}
			case ConfigExt.SITE_ID_WILDBUDDIES:
			{
				return new phoenix.web.locators.wildbuddies.SearchPageLocators();
			}
			case ConfigExt.SITE_ID_FLIRT:
			{
				return new phoenix.web.locators.flirt.SearchPageLocators();
			}
			case ConfigExt.SITE_ID_BENAUGHTY:
			{
				return new phoenix.web.locators.benaughty.SearchPageLocators();
			}
			case ConfigExt.SITE_ID_MATUREDATING:
			{
				return new phoenix.web.locators.maturedating.SearchPageLocators();
			}
			case ConfigExt.SITE_ID_NAUGHTYFEVER:
			{
				return new phoenix.web.locators.naughtyfever.SearchPageLocators();
			}
			default:
			{
				return new phoenix.web.locators.localsgowild.SearchPageLocators();
			}
		}
	}

	public String getMessegeCounter()
	{
		wd().waitForPresence(elements().blockMessagedMeCounter, "Message counter");
		return wd().getText(elements().blockMessagedMeCounter);
	}

	public boolean isMessageCounterPresent()
	{
		return wd().isElementPresent(elements().blockMessagedMeCounter);
	}

	public boolean isMessageCounterDisplayed()
	{
		return wd().isElementDisplayed(elements().blockMessagedMeCounter);
	}

	public void clickMessageMe()
	{
		this.waitForMessageMe();
		wd().click(elements().linkTopMessagedMe);
	}

	public void waitForMessageMe()
	{
		wd().waitForVisibility(elements().linkTopMessagedMe, "MessageMe button");
	}

	public String getBrowseCounter()
	{
		wd().waitForVisibility(elements().blockBrowsedMeCounter, "Browse counter");
		return wd().getText(elements().blockBrowsedMeCounter);
	}

	public void clickBrowsedMe()
	{
		this.waitForBrowsedMe();
		wd().click(elements().linkTopBrowsedMe);
	}

	public void waitForBrowsedMe()
	{
		wd().waitForVisibility(elements().linkTopBrowsedMe, "BrowsedMe button");
	}

	public boolean isBrowseCounterPresent()
	{
		return wd().isElementPresent(elements().blockBrowsedMeCounter);
	}

	public boolean isBrowseCounterDisplayed()
	{
		return wd().isElementDisplayed(elements().blockBrowsedMeCounter);
	}

	public String getWinkCounter()
	{
		wd().waitForPresence(elements().blockWinkedMeCounter, "Wink counter");
		return wd().getText(elements().blockWinkedMeCounter);
	}

	public void clickWinkedMe()
	{
		this.waitForWinkedMe();
		wd().click(elements().linkTopWinkedMe);
	}

	public void waitForWinkedMe()
	{
		wd().waitForVisibility(elements().linkTopWinkedMe, "Winked Me button");
	}

	public boolean isWinkCounterPresent()
	{
		return wd().isElementPresent(elements().blockWinkedMeCounter);
	}

	public boolean isWinkCounterDisplayed()
	{
		return wd().isElementDisplayed(elements().blockWinkedMeCounter);
	}

	public String getFriendsCounter()
	{
		wd().waitForVisibility(elements().blockFavoritsCounter, "Friends counter");
		return wd().getText(elements().blockFavoritsCounter);
	}

	public boolean isFavoritsCounterPresent()
	{
		return wd().isElementPresent(elements().blockFavoritsCounter);
	}

	public boolean isFavoritsCounterDisplayed()
	{
		return wd().isElementDisplayed(elements().blockFavoritsCounter);
	}

	public String getFeedCounter()
	{
		wd().waitForVisibility(elements().blockNewsFeedsCounter, "Feed counter");
		return wd().getText(elements().blockNewsFeedsCounter);
	}

	public boolean isFeedCounterPresent()
	{
		return wd().isElementPresent(elements().blockNewsFeedsCounter);
	}

	public boolean isFeedCounterDisplayed()
	{
		return wd().isElementDisplayed(elements().blockNewsFeedsCounter);
	}

	public void waitForUserNamePresence(TestUserDataModel p_userName)
	{
		wd().waitForVisibility(
			elements().txtScreenName(p_userName.getScreenname()), "Waiting for visible text " + elements().txtScreenName(p_userName.getScreenname()));
	}

	public void clickByUserID(TestUserDataModel p_userName)
	{
		wd().waitForPresence(elements().userById(p_userName.getId()), "Waiting for visible menu " + elements().userById(p_userName.getId()));
		wd().click(elements().userById(p_userName.getId()));
	}

	public void waitForPopUpMessage()
	{
		wd().waitForVisibility(elements().blockMessageAlert, "Message pop-up");
	}

	public void waitForPopUpBrowse()
	{
		wd().waitForVisibility(elements().blockBrowseAlert, "Browse pop-up");
	}

	public void waitForPopUpWink()
	{
		wd().waitForVisibility(elements().blockWinkAlert, "Wink pop-up");
	}

	public void waitForDropMenu()
	{
		wd().waitForPresence(elements().blockDropMenu, "Dropdown menu");
	}

	/**
	 * @param p_counterName | "message", "browse", "wink", "friends", "feed"
	 * @return int or 0 for empty counter
	 */
	public int getCounterValue(String p_counterName)
	{
		String currentUrl = wd().getCurrentUrl();
		String l_value = ""; // String from element
		int l_result; // Int returned value

		// Store counter values
		if (manager.isWB() || manager.isBN())
		{
			log().warn("getCounterValue : We are going to /newsFeed page for reading counters");
			wd().openPage("/newsFeed"); // Goes to news feed page for reading counters
		}

		wd().wait(30);
		wd().refreshPage();
		wd().wait(10);

		switch (p_counterName)
		{
			case "message":

				if (this.isMessageCounterPresent() && this.isMessageCounterDisplayed())
					l_value = this.getMessegeCounter();
				break;
			case "browse":
				if (this.isBrowseCounterPresent() && this.isBrowseCounterDisplayed())
					l_value = this.getBrowseCounter();
				break;
			case "wink":
				if (this.isWinkCounterPresent() && this.isWinkCounterDisplayed())
					l_value = this.getWinkCounter();
				break;
			case "friends":
				if (this.isFavoritsCounterPresent() && this.isFavoritsCounterDisplayed())
					l_value = this.getFriendsCounter();
				break;
			case "feed":
				if (this.isFeedCounterPresent() && this.isFeedCounterDisplayed())
					l_value = this.getFeedCounter();
				break;
		}

		try
		{
			l_result = Integer.parseInt(l_value);
			manager.log().info(p_counterName + " counter found : value " + l_result);
		}
		catch (NumberFormatException p_ex)
		{
			l_result = 0;
			manager.log().warn(p_counterName + " counter NOT found : value " + l_result);
		}
		finally
		{
			log().warn("getCounterValue : Now, we go back to url " + currentUrl);
			wd().openURL(currentUrl);
			wd().wait(3);
		}
		return l_result;
	}

	public void clickAtDropMenuByType(String p_typeOfDropMenu)
	{
		switch (p_typeOfDropMenu)
		{
			case "message":
				this.clickMessageMe();
				break;
			case "browse":
				this.clickBrowsedMe();
				break;
			case "wink":
				this.clickWinkedMe();
				break;
		}
	}

	/**
	 * @param p_alertType | "message", "browse", "wink"
	 * @param p_messageComment | Error message
	 * @return
	 */
	public ActivityPage waitForAlert(String p_alertType, String p_messageComment)
	{
		switch (p_alertType)
		{
			case "message":
				this.waitForPopUpMessage();
				break;

			case "browse":
				this.waitForPopUpBrowse();
				break;

			case "wink":
				this.waitForPopUpWink();
				break;
		}

		manager.log().info("pop-up alert for " + p_alertType + " has been found " + p_messageComment);

		return this;
	}

	/**
	 * @param p_typeOfDropMenu | "message", "browse", "wink"
	 */
	public void waitForDropMenuByType(String p_typeOfDropMenu)
	{
		switch (p_typeOfDropMenu)
		{
			case "message":
				this.waitForMessageMe();
				break;
			case "browse":
				this.waitForBrowsedMe();
				break;
			case "wink":
				this.waitForWinkedMe();
				break;
		}
	}

}
